﻿using System;
using System.Data;
using DataProvider;

namespace Provider
{
    public class BaseProvider
    {
        private readonly IDBManager _idbManager;
        private const int CommandTimeOut = 30;

        public BaseProvider(DataProvider.DataProvider pProvider, string pConnectionString)
        {
            if (pConnectionString != "" && pProvider != DataProvider.DataProvider.None)
                _idbManager = new DBManager(pProvider, pConnectionString);
        }

        public bool TestConnection()
        {
            if (_idbManager == null) return false;
            try
            {
                _idbManager.Open();
                _idbManager.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        private bool IsInitialize()
        {
            return _idbManager != null;
        }

        public IDbDataParameter CreateParameter(string parameterName, object parameterValue)
        {
            return _idbManager.CreateParameter(parameterName, parameterValue);
        }
        public void BeginTransaction()
        {
            _idbManager.BeginTransaction();
        }
        public void CommitTransaction()
        {
            _idbManager.CommitTransaction();
        }
        public void RollBackTransaction()
        {
            _idbManager.RollBackTransaction();
        }
        public DataSet ExecuteDataSet(CommandType pCommandType, string pCommandText, IDbDataParameter[] pParameters, int commandTimeOut = CommandTimeOut)
        {
            if (!IsInitialize()) throw new Exception("DBManager is not initialize");

            _idbManager.SetParameters(pParameters);
            _idbManager.Open();
            DataSet rs = _idbManager.ExecuteDataSet(pCommandType, pCommandText, commandTimeOut);
            _idbManager.Close();
            return rs;
        }

        public DataSet ExecuteDataSet(CommandType pCommandType, string pCommandText, int commandTimeOut = CommandTimeOut)
        {
            return ExecuteDataSet(pCommandType, pCommandText, null, commandTimeOut);
        }

        public int ExecuteNonQuery(CommandType pCommandType, string pCommandText, IDbDataParameter[] pParameters, int commandTimeOut = CommandTimeOut)
        {
            if (!IsInitialize()) throw new Exception("DBManager is not initialize");

            _idbManager.SetParameters(pParameters);
            _idbManager.Open();
            int rs = _idbManager.ExecuteNonQuery(pCommandType, pCommandText, commandTimeOut);
            _idbManager.Close();
            return rs;
        }

        public int ExecuteNonQuery(CommandType pCommandType, string pCommandText, int commandTimeOut = CommandTimeOut)
        {
            return ExecuteNonQuery(pCommandType, pCommandText, null, commandTimeOut);
        }

        public object ExecuteScalar(CommandType pCommandType, string pCommandText, IDbDataParameter[] pParameters, int commandTimeOut = CommandTimeOut)
        {
            if (!IsInitialize()) throw new Exception("DBManager is not initialize");
            _idbManager.SetParameters(pParameters);

            _idbManager.Open();
            var rs = _idbManager.ExecuteScalar(pCommandType, pCommandText, commandTimeOut);
            _idbManager.Close();
            return rs;
        }

        public object ExecuteScalar(CommandType pCommandType, string pCommandText, int commandTimeOut = CommandTimeOut)
        {
            return ExecuteScalar(pCommandType, pCommandText, null, commandTimeOut);
        }

        public IDataReader ExecuteReader(CommandType commandType, string commandText, IDbDataParameter[] parameters, int commandTimeOut = CommandTimeOut)
        {
            if (!IsInitialize()) throw new Exception("DBManager is not initialize");

            _idbManager.SetParameters(parameters);
            _idbManager.Open();
            var result = _idbManager.ExecuteReader(commandType, commandText, commandTimeOut);
            return result;
        }

        public IDataReader ExecuteReader(CommandType commandType, string commandText, int commandTimeOut = CommandTimeOut)
        {
            if (!IsInitialize()) throw new Exception("DBManager is not initialize");
            _idbManager.Open();
            var result = _idbManager.ExecuteReader(commandType, commandText, commandTimeOut);
            return result;
        }
    }
}
